package com.zxy.ider.ehome.util;

import android.content.Context;
import android.util.Log;

import com.zxy.ider.ehome.Constant;
import com.zxy.ider.ehome.FileActivity;
import com.zxy.ider.ehome.db.UpInfo;

import org.litepal.crud.DataSupport;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by Eric on 2017/11/3.
 */

public class SendInfo {

    public static void upInfo(final UpInfo upInfo, Context context){
        final PreferenceManager pm = PreferenceManager.getInstance(context);
        String info = upInfo.getUpInfo();
        Log.i("zxy", "info:" +info);
        RequestParams params = new RequestParams(Constant.LAUNCHER_UPLOAD);
        params.addBodyParameter("data", info);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //加载成功回调，返回获取到的数据
                Log.i("zxy", "onSuccess: upload succes" +result);
                if (result.equals("{\"result\":\"1\"}")){
                    DataSupport.deleteAll(UpInfo.class,"AllInfo = ?",upInfo.getAllInfo());
                }else {
                    upInfo.save();
                    pm.putBoolean("isInfoSave",true);
                    Log.i("zxy", "upInfo.save()=" +upInfo.getAllInfo());
                }
//                        pm.putBoolean("isUpload",true);
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i("zxy", "onSuccess: upload succes" +cex);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("zxy", "onSuccess: upload succes" +ex);
            }
        });
    }
    public static void upDataSupport(final UpInfo upInfo, final List<UpInfo> upInfoList){
        String info = upInfo.getUpInfo();
        Log.i("zxy", "info:" +info);
        RequestParams params = new RequestParams(Constant.LAUNCHER_UPLOAD);
        params.addBodyParameter("data", info);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //加载成功回调，返回获取到的数据
                Log.i("zxy", "onSuccess: upload succes" +result);
                if (result.equals("{\"result\":\"1\"}")){
                    synchronized (UpInfo.class){
                        DataSupport.deleteAll(UpInfo.class,"AllInfo = ?",upInfo.getAllInfo());
                    }
                    synchronized (upInfoList) {
                        upInfoList.remove(upInfo);
                    }
                }
//                        pm.putBoolean("isUpload",true);
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i("zxy", "onSuccess: upload succes" +cex);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("zxy", "onSuccess: upload succes" +ex);
            }
        });
    }
}
